﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Rad_Studio_Keygen
{
    public class RadLicense
    {
        public List<Dictionary<string, string>> Pairs = new List<Dictionary<string, string>>();
        public RadLicense(string str)
        {
            var lines = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            var i = 0;

            while (i < lines.Length)
            {
                bool first = i == 0;
                var c = Convert.ToInt32(lines[i]);
                var dict = new Dictionary<string, string>();
                var j = i + 1;
                for (; j < i + 1 + c * 2; j += 2)
                {
                    dict.Add(lines[j], lines[j + 1]);
                }
                i = j;
                Pairs.Add(dict);
                if (first)
                    i++; // 4
            }
        }

        public enum TermType
        {
            /// <summary>
            /// 企业版
            /// </summary>
            [EnumMember(Value = "Enterprise")]
            Enterprise = 52,
            /// <summary>
            /// 架构师
            /// </summary>
            [EnumMember(Value = "Architect")]
            Architect = 53,
            /// <summary>
            /// 终极版
            /// </summary>
            [EnumMember(Value = "Ultimate")]
            Ultimate
        }

        public static string CreateLicenseText(string SerialNumber, string ActiveCode, RadStudioVersion RadStudioVersion, TermType termType = TermType.Architect)
        {
            var random = new Random();
            var rndkey = GetRndKey();
            var verNum = Convert.ToInt32(RadStudioVersion.Ver.Split('.')[0]);
            var verNumj = verNum - 1;
            var pid = RadStudioVersion.LicHostPID.ToString();
            var skuid = RadStudioVersion.LicHostSKU.ToString();
            string Result = "";
            if (verNum > 27)
            {
                Result = $@"11
e.pkg
Embarcadero RAD Studio {RadStudioVersion.LicVerStr} Architect
e.pt
10
e.sign
0
e.sign2
0
e.sign3
0
export.allowed
0
import.allowed
1
import.silent
1
licensed.serialno
{SerialNumber}
nodelock.node
0
nodelock.session
{ActiveCode}
2
{verNumj}
active
T
beta
0
exportable
0
hostpid
{pid}
hostskuid
{skuid}
internaluse
0
naggy
0
noncommercial
0
noncommercial_label
No
platform
1
platform_label
Windows
product
2000
productid
{RadStudioVersion.LicDelphiPID}
productid_label
Delphi 11
productsku
{skuid}
productsku_label
Architect
rndkey
{rndkey}
serialno
{SerialNumber}
sku
{skuid}
templicense
0
termtype
0
termtype_label
Unlimited
title
Delphi {RadStudioVersion.LicVerStr} Architect
trial
0
upgrade
0
version
{verNum}
xpdate
1631247963000
30
Android
T
Android64
T
DESIGNDIAGRAMS
TRUE
DESIGNPROJECTS
TRUE
Desktop
T
FULLQA
TRUE
FirstRegistered
1631247963000
FulliOS
T
Linux64
T
MODELLING
TRUE
Mobile
T
OSX32
T
OSX64
T
Win32
T
Win64
T
a100
MakeThingsHappen
a1000
PrintMoreMoney
a101
ImGivinItAllShesGot
a200
StampIt
a250
ItsToolTimeBaby
a300
TheMalteseFalcon
a301
GlueSolvent
contactid
10655361
crd
1631247963000
hostsuite
{pid}
iOSDevice32
T
iOSDevice64
T
iOSSimulator
T
updatelevel
0.0
updates
1
{verNumj}
active
T
beta
0
exportable
0
hostpid
{pid}
hostskuid
{skuid}
internaluse
0
naggy
0
noncommercial
0
noncommercial_label
No
platform
1
platform_label
Windows
product
4000
productid
{RadStudioVersion.LicCBuilderPID}
productid_label
C++Builder 11
productsku
{skuid}
productsku_label
Architect
rndkey
{rndkey}
serialno
{SerialNumber}
sku
{skuid}
templicense
0
termtype
0
termtype_label
Unlimited
title
C++Builder {RadStudioVersion.LicVerStr} Architect
trial
0
upgrade
0
version
21
xpdate
1631247963000
29
Android
T
Android64
T
DESIGNDIAGRAMS
TRUE
DESIGNPROJECTS
TRUE
Desktop
T
FULLQA
TRUE
FirstRegistered
1631247963000
FulliOS
T
MODELLING
TRUE
Mobile
T
OSX32
T
OSX64
T
Win32
T
Win64
T
a100
MakeThingsHappen
a1000
PrintMoreMoney
a101
ImGivinItAllShesGot
a200
StampIt
a250
ItsToolTimeBaby
a300
TheMalteseFalcon
a301
GlueSolvent
contactid
10655361
crd
1631247963000
hostsuite
{pid}
iOSDevice32
T
iOSDevice64
T
iOSSimulator
T
updatelevel
0.0
updates
1
";
            }
            else
            {
                Result = "11\n";
                Result += "e.pkg\nEmbarcadero RAD Studio " + RadStudioVersion.LicVerStr + " Architect\n";
                Result += "e.pt\r\n10\r\n";
                Result += "e.sign\r\n0\r\n";
                Result += "e.sign2\r\n0\r\n";
                Result += "e.sign3\r\n0\r\n";
                Result += "export.allowed\n0\n";
                Result += "import.allowed\n1\n";
                Result += "import.silent\n1\n";
                Result += "licensed.serialno\n" + SerialNumber + "\n";
                Result += "nodelock.node\n0\n";
                Result += "nodelock.session\n" + ActiveCode + "\r\n";

                Result += "4\n";

                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n1\n";
                Result += "platform_label\nWindows\n";
                Result += "product\n2000\n";
                Result += "productid\n" + RadStudioVersion.LicDelphiPID + "\n";
                Result += "productid_label\nDelphi " + RadStudioVersion.LicVerStr + "\n";
                Result += "productsku\n" + skuid + "\n";
                Result += "productsku_label\nArchitect\n";
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n" + skuid + "\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nUnlimited\n";
                Result += "title\nDelphi " + RadStudioVersion.LicVerStr + " Architect\n";
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n27\n";
                Result += "27\n";
                Result += "Android\nT\n";
                Result += "DESIGNDIAGRAMS\nTRUE\n";
                Result += "DESIGNPROJECTS\nTRUE\n";
                Result += "Desktop\nT\n";
                Result += "FULLQA\nTRUE\n";
                Result += "FulliOS\nT\n";
                Result += "Linux64\nT\n";
                Result += "MODELLING\nTRUE\n";
                Result += "Mobile\nT\n";
                Result += "OSX32\nT\n";
                Result += "OSX64\nT\n";
                Result += "Win32\nT\n";
                Result += "Win64\nT\n";
                Result += "a100\nMakeThingsHappen\n";
                Result += "a1000\nPrintMoreMoney\n";
                Result += "a101\nImGivinItAllShesGot\n";
                Result += "a200\nStampIt\n";
                Result += "a250\nItsToolTimeBaby\n";
                Result += "a300\nTheMalteseFalcon\n";
                Result += "a301\nGlueSolvent\n";
                Result += "hostsuite\n" + pid + "\n";
                Result += "iOSDevice\nT\n";
                Result += "iOSDevice32\nT\n";
                Result += "iOSDevice64\nT\n";
                Result += "iOSSimulator\nT\n";
                Result += "updatelevel\n0.0\n";
                Result += "updates\n1\n";

                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n1\n";
                Result += "platform_label\nWindows\n";
                Result += "product\n4000\n";
                Result += "productid\n" + RadStudioVersion.LicCBuilderPID + "\n";
                Result += "productid_label\nC++Builder " + RadStudioVersion.LicVerStr + "\n";
                Result += "productsku\n" + skuid + "\n";
                Result += "productsku_label\nArchitect\n";               //Architect
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n" + skuid + "\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nUnlimited\n";
                Result += "title\nC++Builder " + RadStudioVersion.LicVerStr + " Architect\n";
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n20\n";
                Result += "25\n";
                Result += "Android\nT\n";
                Result += "DESIGNDIAGRAMS\nTRUE\n";
                Result += "DESIGNPROJECTS\nTRUE\n";
                Result += "Desktop\nT\n";
                Result += "FULLQA\nTRUE\n";
                Result += "FulliOS\nT\n";
                Result += "Linux64\nT\n";
                Result += "MODELLING\nTRUE\n";
                Result += "Mobile\nT\n";
                Result += "OSX32\nT\n";
                Result += "Win32\nT\n";
                Result += "Win64\nT\n";
                Result += "a100\nMakeThingsHappen\n";
                Result += "a1000\nPrintMoreMoney\n";
                Result += "a101\nImGivinItAllShesGot\n";
                Result += "a200\nStampIt\n";
                Result += "a250\nItsToolTimeBaby\n";
                Result += "a300\nTheMalteseFalcon\n";
                Result += "a301\nGlueSolvent\n";
                Result += "hostsuite\n" + pid + "\n";
                Result += "iOSDevice32\nT\n";
                Result += "iOSDevice64\nT\n";
                Result += "iOSSimulator\nT\n";
                Result += "updatelevel\n0.0\n";
                Result += "updates\n1\n";


                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n0\n";
                Result += "platform_label\nCross Platform\n";
                Result += "product\n7000\n";
                Result += "productid\n7113\n";
                Result += "productid_label\nInterBase 2020\n";        //InterBase XE7
                Result += "productsku\n0\n";
                Result += "productsku_label\nServer\n";               //Server
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n0\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nUnlimited\n";
                Result += "title\nInterBase 2020 Server\n";      //InterBase XE7 Server
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n8\n";
                Result += "21\n";
                Result += "changeView\n1\n";
                Result += "connectionMonitoring\n1\n";
                Result += "connectionsPerUser\n200\n";
                Result += "customVarId\n \n";
                Result += "databaseAccess\n1\n";
                Result += "dbEncryption\n1\n";
                Result += "ddlOperations\n1\n";
                Result += "devLicense\n1\n";
                Result += "externalFileAccess\n1\n";
                Result += "internetAccess\n1\n";
                Result += "languages\nALL\n";
                Result += "licensedCpus\n32\n";
                Result += "licensedUsers\n5000\n";
                Result += "nodeID\n \n";
                Result += "otwEncryption\n1\n";
                Result += "remoteAccess\n1\n";
                Result += "serverAccess\n1\n";
                Result += "togoAccess\n0\n";
                Result += "updatelevel\n0.0\n";
                Result += "useAddons\n0\n";
                Result += "version\n14.0\n";


                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n0\n";
                Result += "platform_label\nCross Platform\n";
                Result += "product\n7000\n";
                Result += "productid\n7113\n";
                Result += "productid_label\nInterBase 2020\n";        //InterBase XE7
                Result += "productsku\n16\n";
                Result += "productsku_label\nToGo Edition\n";               //ToGo Edition
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n16\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nUnlimited\n";
                Result += "title\nInterBase 2020 ToGo Edition\n";      //InterBase XE7 ToGo Edition
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n8\n";
                Result += "21\n";
                Result += "changeView\n1\n";
                Result += "connectionMonitoring\n1\n";
                Result += "connectionsPerUser\n200\n";
                Result += "customVarId\n \n";
                Result += "databaseAccess\n1\n";
                Result += "dbEncryption\n1\n";
                Result += "ddlOperations\n1\n";
                Result += "devLicense\n1\n";
                Result += "externalFileAccess\n1\n";
                Result += "internetAccess\n1\n";
                Result += "languages\nALL\n";
                Result += "licensedCpus\n32\n";
                Result += "licensedUsers\n5000\n";
                Result += "nodeID\n \n";
                Result += "otwEncryption\n1\n";
                Result += "remoteAccess\n1\n";
                Result += "serverAccess\n1\n";
                Result += "togoAccess\n1\n";
                Result += "updatelevel\n0.0\n";
                Result += "useAddons\n0\n";
                Result += "version\n14.0\n1\n";
                Result += "updatelevel\n0.0\n";

                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n1\n";
                Result += "platform_label\nWindows\n";
                Result += "product\n14100\n";
                Result += "productid\n14110\n";
                Result += "productid_label\nER/Studio 2016\n";
                Result += "productsku\n15\n";
                Result += "productsku_label\nDeveloper MultiPlatform\n"; //Developer MultiPlatform
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n15\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nPermanent\n";
                Result += "title\nER/Studio Developer 2016\n";
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n10\n";
                Result += "30\n";
                Result += "CaErwin3Erx\nT\n";
                Result += "CaErwin3ErxImport.MicrosoftOfficeVisio\nT\n";
                Result += "CaErwin4Xml\nT\n";
                Result += "CaErwin7ModelManager\nT\n";
                Result += "CaErwin7Xml\nT\n";
                Result += "CaErwin8ModelManager\nT\n";
                Result += "CaErwin8Xml\nT\n";
                Result += "CaErwin9Mart\nT\n";
                Result += "CaErwin9Xml\nT\n";
                Result += "CaMart9\nT\n";
                Result += "CaModelManager7\nT\n";
                Result += "CaModelManager8\nT\n";
                Result += "CrossPlatform\nT\n";
                Result += "IbmInfoSphereDiscoveryImport.CaErwinDataProfiler\nT\n";
                Result += "IbmRationalDataArchitect\nT\n";
                Result += "IbmRationalDataArchitectImport.IbmDb2DataWarehouse\nT\n";
                Result += "IbmRationalRose98Import.MicrosoftVisualStudioModeler\nT\n";
                Result += "OmgCwmXmiImport.CaErwinDataProfiler\nT\n";
                Result += "OmgUml2XmiImport.PowerDesignerOom\nT\n";
                Result += "OmgUml2XmiImport.SparxEA\nT\n";
                Result += "OmgUmlXmiImport.SparxEA\nT\n";
                Result += "OmgUmlXmiImport.SybasePowerDesignerOom\nT\n";
                Result += "OracleDesigner\nT\n";
                Result += "SybasePowerDesignerCdm06\nT\n";
                Result += "SybasePowerDesignerCdm07\nT\n";
                Result += "SybasePowerDesignerLdm15\nT\n";
                Result += "SybasePowerDesignerPdm07\nT\n";
                Result += "baseLicense\nDeveloper\n";
                Result += "updatelevel\n0.0\n";
                Result += "updates\n1\n";


                Result += "26\n";
                Result += "active\nT\n";
                Result += "beta\n0\n";
                Result += "exportable\n0\n";
                Result += "hostpid\n" + pid + "\n";
                Result += "hostskuid\n" + skuid + "\n";
                Result += "internaluse\n0\n";
                Result += "naggy\n0\n";
                Result += "noncommercial\n0\n";
                Result += "noncommercial_label\nNo\n";
                Result += "platform\n1\n";
                Result += "platform_label\nWindows\n";
                Result += "product\n2700\n";
                Result += "productid\n2705\n";
                Result += "productid_label\nHTML5 Builder\n";         //HTML5 Builder
                Result += "productsku\n0\n";
                Result += "productsku_label\nRadPHP\n";               //RadPHP
                Result += $"rndkey\n{rndkey}\n";
                Result += "serialno\n" + SerialNumber + "\n";
                Result += "sku\n0\n";
                Result += "templicense\n0\n";
                Result += "termtype\n0\n";
                Result += "termtype_label\nPermanent\n";
                Result += "title\nHTML5 Builder\n";                    //HTML5 Builder
                Result += "trial\n0\n";
                Result += "upgrade\n0\n";
                Result += "version\n5\n";
                Result += "1\n";
                Result += "updatelevel\n0.0\n";
            }

            return Result;
        }


        static uint randomBase = 0x0D138F2E3;
        public static uint RandomNumber()
        {
            randomBase = 0x8088405 * randomBase + 1;
            return randomBase;
        }

        public static uint sub_407334(uint a1)
        {
            ulong tmp = a1 * (ulong)RandomNumber();
            return (uint)(tmp >> 32);
        }

        public static string GetRndKey()
        {
            var v3 = sub_407334(0x20) << 16;
            var v4 = sub_407334(0x20);
            return ((v4 + v3) % 89999999 + 10000000).ToString();
        }
    }
}
